package com.nchuoj.question.config;

import com.feign.config.enums.ExchangeNameEnum;
import com.feign.config.enums.QueueNameEnum;
import com.feign.config.enums.RouteKeyEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * TODO
 *
 * @Description
 * @Author ZW
 * @Date 2022/12/21 11:13
 **/
@Configuration
@Slf4j
public class JudgeMq {
    /**
     * 创建判题队列
     *
     * @return 注入的队列对象
     */
    @Bean("judgeQueue")
    public Queue createJudgeQueue() {
        log.info("创建判题队列");
        return new Queue(QueueNameEnum.JUDGE_QUEUE.getName(), true, false, false);
    }

    /**
     * 创建 qa 的消息队列
     */
    @Bean("qaQueue")
    public Queue createQaQueue() {
        log.info("创建QA队列");
        return new Queue(QueueNameEnum.QA_QUEUE.getName(), true, false, false);
    }

    /**
     * 创建 judge -> question服务的队列
     */
    @Bean("judge2QuestionQueue")
    public Queue createJudge2QuestionQueue() {
        log.info("创建judge->question队列");
        return new Queue(QueueNameEnum.JUDGE_2_QUESTION_QUEUE.getName(), true, false, false);
    }

    /**
     * 创建题目服务交换机
     */
    @Bean("questionDirectExchange")
    public DirectExchange createQuestionDirectExchange() {
        log.info("创建题目路由");
        return new DirectExchange(ExchangeNameEnum.QUESTION_DIRECT_EXCHANGE.getName(), true, false);
    }

    @Bean
    public Binding bindingQuestionFanoutExchangeToJudgeQueue(Queue judgeQueue, DirectExchange questionDirectExchange) {
        return BindingBuilder.bind(judgeQueue).to(questionDirectExchange).with(RouteKeyEnum.QUESTION_2_JUDGE_AND_QA);
    }

    @Bean
    public Binding bindingQuestionFanoutExchangeToQaQueue(Queue qaQueue, DirectExchange questionDirectExchange) {
        return BindingBuilder.bind(qaQueue).to(questionDirectExchange).with(RouteKeyEnum.QUESTION_2_JUDGE_AND_QA);
    }

    @Bean
    public Binding bindingQuestionFanoutExchangeToJudge2QuestionQueue(Queue judge2QuestionQueue, DirectExchange questionDirectExchange) {
        return BindingBuilder.bind(judge2QuestionQueue).to(questionDirectExchange).with(RouteKeyEnum.JUDGE_2_QUESTION);
    }

}
